﻿/**
* autor Zero
* version 1.1
* Se ha eliminado la posibilidad de 3 tamaños: ahora son sólo 2
* Se agrega un getMessages que se le hace a CENTRALBridge.as
* version 1.3
* Implementa la interface Draggable, teniendo que definir un area de drag	
* Resuelve de forma especial el startDrag, y se agrega un updateAfterEvent() para que sea mas suave el movimiento
* Incorpora la clase Buzz para hacer el fucking tembleke
* Cuando consulta a CENTRALBridge cosas para el DATABridge, le pasa una referencia del DATABridge
* version 1.4
* Al DATABridge le hace un setType en vez de clavarlo de una
*/

import com.dinmax.flash.draw.common.*
import com.dinmax.flash.draw.cabeza.ControlCabeza
import com.dinmax.flash.draw.utilities.Buzz

import com.dinmax.flash.central.bridge.*
import com.dinmax.flash.central.draw.*
import com.dinmax.flash.central.extension.draw.ExtensionPreferences
import com.dinmax.flash.central.extension.message.*

class EXTENSIONBridge
extends ControlCabeza 
implements com.dinmax.flash.draw.drag.Draggable {
	
	public static var nTIP_TIME:Number=600; // tiempo necesario para el tooltip;
	public static var nTOOLTIP_Y:Number=30; // offset en Y del tooltip;
	
	public static var nSMALLER:Number = 0;
	public static var nSMALL:Number = 1;
	public static var nLARGE:Number = 2;
	public var nWINDOW_SIZE:Number = nSMALLER;

	public static var _USER:Number=0; 
	public static var _LOG:Number=1; 
	public static var _MESSAGES:Number=2;
	public static var _VOICEMAIL:Number=3;
	public static var _PREF:Number=4;
	
	private var bridge:CENTRALBridge;
	private var extensionState:String;
	private var buzz:Buzz;
		
	private var parent:MovieClip;
	private var mcData:DATABridge; // es el clip que creo cuando hago pantallita
	private var dataLoading:Boolean= false; //indica si se traba o no
	private var limitArea:Area; // area limite para el drag

	//CLIPS ON STAGE
	private var mcFondo:MovieClip;
	private var mcButtonBar:EXTENSIONButtonsBridge;
	private var mcDisabled:MovieClip;
	private var mcMarquee:MovieClip;
	private var mcLedState:MovieClip;
	private var mcLedTalk:MovieClip;
	private var txt_talk:TextField;
	private var txt_state:TextField;
	//
	
	function EXTENSIONBridge(){
		this.gotoAndStop("smaller");
	};
	
	function onLoad():Void {
		super.onLoad(); // hace que se registre con el padre si es un control cabeza
		//trace("EXTENSIONBridge.as onLoad()");
		bridge.EXTENSIONBridge_2_CENTRALBridge_getState(this);
		this.init();
		//this.resetData();
		return;
	}; //fin funci
	
	
	public function startDrag():Void{
		super.startDrag(false,getLimitArea().getInitXY().getX(), /*left*/
			getLimitArea().getInitXY().getY(), /*top*/
			getLimitArea().getFinalXY().getX()-this._width, /*right*/
			getLimitArea().getFinalXY().getY()-this._height /*bottom*/
		);
		updateAfterEvent();
	};
	
	private function init():Void {
		
		if(!this.buzz){
			this.buzz = new Buzz(this);
		};
		
		mcFondo.root = this;
		mcFondo.onPress=function():Void{
			this.root.startDrag();
			DepthManager.getActive().toFront(this.root);
			if(getTimer()- this.timer < 500){
				this.onRelease();
				this.root.setSize(this.root.nWINDOW_SIZE);
				this.timer = undefined;
			} else {
				this.timer = getTimer();
			}
			return;
		};

		mcFondo.onRelease=function():Void{
			this.root.stopDrag();
			return;
		};
		mcFondo.onReleaseOutside=function():Void{
			this.onRelease();
			return;
		};
		return;	
	};
	
	private function registerChild(clip:MovieClip):Void{
		switch(clip){
			case this.mcButtonBar:
			this.refreshButtonBar();
			break;
		};
		return;
	};
	private function refreshButtonBar():Void{
		mcButtonBar.lightMessage(this.getNewTextMessages() > 0);
		mcButtonBar.lightVoiceMail(this.getNewVoiceMessages() > 0);
		return;
	};
	
	// MARQUEE TEXT
		
	public function setText(msg:String):Void {
		mcMarquee.setText(msg);
		return;
	}; //fin funci
	public function setAnimateText(msg:String):Void {
		mcMarquee.setAnimateText(msg);
		return;
	};
	
	/// DATA 
	
	public function writeMessage(to:Number):Void {
		//trace("EXTENSIONBridge.as writeMessage() to="+ to);
		this.createData(DATABridge.MESSAGE_SENDER, to);
		return;
	};
	
	private function createData(type:String, value:Object):Void {
		if(!dataLoading){
			dataLoading = true;
			// le clavo el type de prepo, me parece medio punga
			this.mcData = DATABridge(this.attachMovie("Data", "mcData", this.getNextHighestDepth()));
			this.mcData.setType(type);
			this.mcData.setValue(value);
		} else {
			// si el data está cargandose, lo reseteo y ahí lo creo de nuevo
			this.clearData();
			this.dataLoaded();
			this.createData(type, value);
		};
		return;
	};

	private function clearData():Void {
		com.dinmax.flash.utilities.movieclip.MovieClipUtils.destroyClip(this.mcData);
		return;
	};
	
	private function dataLoaded():Void{
		dataLoading = false;
		return;
	};
	
	public function resetData():Void {
		if(mcData._currentframe == 0){ 
			//trace("EXTENSIONBridge.as | resetData() data.onLoad()");
			mcData.onLoad();
		} else {
			//trace("EXTENSIONBridge.as | resetData() data.refreshData()");
			//mcData.endInterpolation();
			mcData.refreshData();
		};
		return;
	};
	
	// DRAGGABLE
		
	public function getLimitArea():Area {
		return limitArea;
	};

	public function setLimitArea(limitArea:Area):Void {
		this.limitArea = limitArea;
		return;
	};
	
	// CUESTIONES DE ESTADO
	
	public function setDisabled(label:String):Void {
		//trace("EXTENSIONBridge.as | setDisabled()");
		mcDisabled._visible=true;
		txt_state.text="Offline";
		txt_talk.text="Inactive";
		mcLedState.gotoAndStop("red");
		mcLedTalk.gotoAndStop("off");
		this.setText(label);
		if(buzz.getIsBuzzing())	buzz.endBuzz();
		
	}; //fin funci
	public function setEnabled(label:String):Void {
		//trace("EXTENSIONBridge.as | setEnabled()");
		mcDisabled._visible=false;
		txt_state.text="Online";
		txt_talk.text="Inactive";
		mcLedState.gotoAndStop("green");
		mcLedTalk.gotoAndStop("off");
		this.setText(label)
		if(buzz.getIsBuzzing())	buzz.endBuzz();
	}; //fin funci
	public function setCalling(extension:String):Void {
		mcDisabled._visible=false;
		txt_state.text="Online";
		txt_talk.text="Calling";
		mcLedState.gotoAndStop("green");
		mcLedTalk.gotoAndPlay("tiltOrange");
		this.setAnimateText("Calling to "+extension);
		if(buzz.getIsBuzzing())	buzz.endBuzz();
	};
	public function setRinging(extension:String):Void {
		mcDisabled._visible=false;
		txt_state.text="Online";
		txt_talk.text="Ringing";
		mcLedState.gotoAndStop("green");
		mcLedTalk.gotoAndPlay("tiltOrange");
		this.setAnimateText("Incoming call from "+extension);
		this.buzz.startBuzz();
	};
	public function setTalking(extension:String):Void {
		//trace("EXTENSIONBridge.as | setTalking()");
		mcDisabled._visible=false;
		txt_state.text="Online";
		txt_talk.text="Talking";
		mcLedTalk.gotoAndPlay("tiltGreen");
		this.setAnimateText("Talking with "+extension);
		if(buzz.getIsBuzzing())	buzz.endBuzz();
	}; //fin funci	
	
	/// ACTIVE EXTENSION
	private function isExtensionAutorized():Boolean {
		return bridge.isExtensionAutorized(this);
	};
	
	/// USER
	private function getUserData():Void {
		bridge.getUserData(this, mcData);
	};
	
	private function getUserContacts():Void {
		bridge.getUserContacts(this, mcData);	
		return;
	};
		
	/// LOGS
	private function getLog():Void {
		bridge.getLog(this, mcData);
		return;
	};
			
	/// TEXT MESSGAGE
	private function getNewTextMessages():Number {
		return bridge.getNewTextMessages(this)
	};
	
	private function getTextMessages():Void {
		bridge.getTextMessages(this, mcData);
		return;
	};
	private function readMessage(msg:Message):Void{
		bridge.readMessage(msg, this);
		return;
	};
	private function deleteMessage(msg:Message):Void{
		bridge.deleteMessage(msg, this);
		return;
	};
	
		
	/// VOICE MESSAGE
	private function getNewVoiceMessages():Number {
		return bridge.getNewVoiceMessages(this);
	};	
	private function getVoiceMessages():Void {
		bridge.getVoiceMessages(this, mcData);
		return;
	};
	private function refreshVoiceMessages():Void {
		bridge.refreshVoiceMessages(this);
		return;
	};
	
	/// SIZE
		
	public function setSize(size:Number):Void {
		switch(size){
			case nSMALLER:
			this.gotoAndStop("large");
			this.nWINDOW_SIZE = nLARGE;
			break;
			
			case nSMALL:
			this.gotoAndStop("large");
			this.nWINDOW_SIZE = nLARGE;
			break;
			
			case nLARGE:
			this.gotoAndStop("smaller");
			this.nWINDOW_SIZE = nSMALLER;
			break;
		}
		this.onLoad();
		
		return;
	};
	
	/// BRIDGE
	
	public function getBridge():CENTRALBridge {
		return bridge;
	}

	public function setBridge(bridge:CENTRALBridge):Void {
		//trace("EXTENSIONBridge.as setBridge() bridge= "+bridge);
		this.bridge = bridge;
	}

}